import java.util.Random;

//库存管理
public class GetLeastNumbers {
    public static int[] code(int[] stock, int cnt) {
        qsort(stock, 0, stock.length - 1, cnt);

        int[] ret = new int[cnt];
        for (int i = 0; i < cnt ; i++){
            ret[i] = stock[i];
        }
        return ret;
    }

    public static void qsort(int[] nums, int l, int r, int k){
        if (l >= r){
            return;
        }

        int left = l - 1, right = r + 1, i = l;
        int key = nums[new Random().nextInt(r - l + 1) + l];
        while (i < right){
            if (nums[i] < key){
                swap(nums, ++left, i++);
            }else if (nums[i] == key){
                i++;
            }else {
                swap(nums, --right, i);
            }
        }

        int b = right - left - 1, a = left - l + 1;
        if (a > k){
            qsort(nums, l, left, k);
        }else if (a + b >= k){
            return;
            //qsort(nums, left + 1, right - 1, k - a);
        }else{
            qsort(nums, right, r, k - a - b);
        }
    }

    public static void swap(int[] nums, int i, int j){
        int tmp = nums[i];
        nums[i] = nums[j];
        nums[j] = tmp;
    }

    public static void main(String[] args) {
        GetLeastNumbers.code(new int[]{2, 5, 7, 4}, 1);
    }
}
